package com.lanou.dllo.baisibudejie.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lanou.dllo.baisibudejie.R;
import com.lanou.dllo.baisibudejie.adapater.MyNewsRecycleAdapter;
import com.lanou.dllo.baisibudejie.db.MySQLiteMyNewsUtil;

/**
 * Created by dllo on 15/11/3.
 */
public class MyNewsFragment extends Fragment implements View.OnClickListener {
    private ImageView mynews_recommend_followIv;
    private TextView mynews_recommend_followTv;
    private RecyclerView recyclerView;
    private RelativeLayout relativeLayout;
    private MyNewsRecycleAdapter adapter;
    private MySQLiteMyNewsUtil util;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mynews, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        util = new MySQLiteMyNewsUtil(getActivity());
        mynews_recommend_followIv = (ImageView) view.findViewById(R.id.mynews_recommend_followIv);
        mynews_recommend_followIv.setOnClickListener(this);
        mynews_recommend_followTv = (TextView) view.findViewById(R.id.mynews_recommend_followTv);
        mynews_recommend_followTv.setOnClickListener(this);
        relativeLayout = (RelativeLayout) view.findViewById(R.id.relativeLayout);
        recyclerView = (RecyclerView) view.findViewById(R.id.myNews_recyclerView);
        adapter = new MyNewsRecycleAdapter(getActivity());
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 1);
        recyclerView.setLayoutManager(manager);

        if (util.queryMyNews().size()>0) {
            relativeLayout.setVisibility(View.GONE);
            recyclerView.setAdapter(adapter);
            adapter.addData(util.queryMyNews());
        }else {
            relativeLayout.setVisibility(View.VISIBLE);
        }


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onClick(View v){
        switch (v.getId()) {
            case R.id.mynews_recommend_followIv:
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.tabcontent_news, new RecommendFragment());
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
                ft.commit();
                break;
            case R.id.mynews_recommend_followTv:
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.tabcontent_news, new RecommendFragment());
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
                transaction.commit();
                break;
        }
    }
}